﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace scoketClient
{
    public partial class ClientForm : Form
    {
        private TcpClient tcpc;//创建TCP连接
        private Socket stSend;//创建发送数据套接字
        private bool tcpConnect = false;//定义标识，判断TCP是否连接
        public ClientForm()
        {
            InitializeComponent();
            this.tbxServerIP.Text = "192.168.6.147";
            this.tbxPort.Text = "3505";
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            int port = Convert.ToInt32(this.tbxPort.Text);
            try
            {
                stSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint tempRemoteIP = new IPEndPoint(IPAddress.Parse(tbxServerIP.Text), port);
                EndPoint epTemp = (EndPoint)tempRemoteIP;

                stSend.Connect(epTemp);
                Thread clientService = new Thread(new ThreadStart(ReciveMessage));              //启动一个线程用来服务些SOCKET对象(客户端)
                clientService.IsBackground = true;                                              //线程声明为后台线程
                clientService.Start();

                if (stSend.Connected)
                    this.statusBar1.Text = "与" + tbxServerIP.Text.ToString() + "成功";
                else
                    this.statusBar1.Text = "与" + tbxServerIP.Text.ToString() + "建立连接……";

                tcpConnect = true;
                btnConnect.Enabled = false;
                btnSend.Enabled = true;
                btnClose.Enabled = true;


                Byte[] byteSend = new byte[1024];
                byteSend = System.Text.Encoding.UTF8.GetBytes("LOD");

                int i = stSend.Send(byteSend);

            }
            catch (Exception ex)
            {
                statusBar1.Text = ex.Message;
            }
        }

        private void ReciveMessage()
        {
            while (true)
            {
                try
                {
                    Byte[] byBuff = new Byte[8192];                                     //声明缓冲区大小
                    stSend.Receive(byBuff, byBuff.Length, 0);
                    string strBuff = Encoding.UTF8.GetString(byBuff).TrimEnd('\0');
                    if (strBuff.IndexOf("\r\n") > 0)
                    {
                        this.lsbMessage.Items.Add(strBuff);
                    }
                }
                catch
                {
                }
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            int msgLength = tbxMessage.Text.Length;
            Byte[] byteSend = new byte[msgLength];
            byteSend = System.Text.Encoding.UTF8.GetBytes(tbxMessage.Text + "\r\n");

            int i = stSend.Send(byteSend);
            this.lsbMessage.Items.Add(Dns.GetHostName() + ":" + tbxMessage.Text);
            this.tbxMessage.Text = "";
        }

        private void btnClose_Click(object sender, EventArgs e)
        {

            Byte[] byteSend = new byte[1024];
            byteSend = System.Text.Encoding.UTF8.GetBytes("OFF");

            int i = stSend.Send(byteSend);

            stSend.Disconnect(false);
            btnConnect.Enabled = true;


        }
    }
}